﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Data;
using Restaurant.DTO;
using Restaurant.DAO;

namespace Restaurant.BUS
{
    public class NhapHangService
    {
        NhapHangDB nvDAO = new NhapHangDB();

        public DataTable LayNhapHang()
        {
            return nvDAO.LayNhapHang();
        }

        public NhapHang LayNhapHangTheoMa(int maNhapHang)
        {
            DataTable dbtab = nvDAO.LayNhapHangTheoMa(maNhapHang);
            NhapHang nh = new NhapHang();
            DataRow dbrow = dbtab.Rows[0];
            if (dbrow[0] != null)
                nh.MANHAPHANG = int.Parse(dbrow[0].ToString());
            if (dbrow[1] != null)
                nh.NGAYNHAP = DateTime.Parse(dbrow[1].ToString());
            if (dbrow[2] != null)
                nh.TONGTIEN = int.Parse(dbrow[2].ToString());
            if (dbrow[3] != null)
                nh.MAKHO = int.Parse(dbrow[3].ToString());
            if (dbrow[4] != null)
                nh.MAHTTOAN = int.Parse(dbrow[4].ToString());
            if (dbrow[5] != null)
                nh.TINHTRANG = int.Parse(dbrow[5].ToString());
            if (dbrow[6] != null)
                nh.MANCC = int.Parse(dbrow[6].ToString());
            if (dbrow[7] != null)
                nh.NGAYTHANHTOAN = DateTime.Parse(dbrow[7].ToString());
            //Thieu chi tiet nhap hang
            return nh;
        }

        
        public bool ThemNhapHang(NhapHang nh)
        {
            if (nh.MAHTTOAN.ToString() == "" || nh.MAKHO.ToString() == "" || nh.MANCC.ToString() == "" || nh.NGAYNHAP.ToString() == "" || nh.NGAYTHANHTOAN.ToString() == "" || nh.TINHTRANG.ToString() == "" || nh.TONGTIEN.ToString() == "")
            {
                return false;
            }
            if (nvDAO.ThemNhapHang(nh))
                return true;
            return false;
        }

        public bool ThemCTNhapHang(NhapHang nh)
        {
            bool flag = true;
            for (int i = 0; i < nh.CHITIETNHAP.Rows.Count; i++)
            {
                if (!nvDAO.ThemCTNhapHang(nh.MANHAPHANG, int.Parse(nh.CHITIETNHAP.Rows[i]["MaNL"].ToString()), float.Parse(nh.CHITIETNHAP.Rows[i]["SoLuong"].ToString()), int.Parse(nh.CHITIETNHAP.Rows[i]["DonGia"].ToString()), int.Parse(nh.CHITIETNHAP.Rows[i]["ThanhTien"].ToString()), nh.MAKHO))
                    flag = false;
            }
            if (!flag)
            {
                nvDAO.XoaCTNhapHang(nh.MANHAPHANG);
            }
                return flag;
        }

        public bool SuaNhapHang(NhapHang nh)
        {
            if (nvDAO.SuaNhapHang(nh))
                return true;
            return false;
        }

        public bool XoaNhapHang(int ma)
        {
            if (nvDAO.XoaNhapHang(ma))
                return true;
            return false;
        }

        public int TaoMaNhapHang()
        {
            return nvDAO.TaoMaNhapHang();
        }

        public bool ThemCTNhapHangDeadLockError(NhapHang nh)
        {
            bool flag = true;
            for (int i = 0; i < nh.CHITIETNHAP.Rows.Count; i++)
            {
                if (!nvDAO.ThemCTNhapHangDeadLockError(nh.MANHAPHANG, int.Parse(nh.CHITIETNHAP.Rows[i]["MaNL"].ToString()), float.Parse(nh.CHITIETNHAP.Rows[i]["SoLuong"].ToString()), int.Parse(nh.CHITIETNHAP.Rows[i]["DonGia"].ToString()), int.Parse(nh.CHITIETNHAP.Rows[i]["ThanhTien"].ToString()), nh.MAKHO))
                    flag = false;
            }
            if (!flag)
            {
                nvDAO.XoaCTNhapHang(nh.MANHAPHANG);
            }
            return flag;
        }

        public bool ThemCTNhapHangDeadLock(NhapHang nh)
        {
            bool flag = true;
            for (int i = 0; i < nh.CHITIETNHAP.Rows.Count; i++)
            {
                if (!nvDAO.ThemCTNhapHangDeadLock(nh.MANHAPHANG, int.Parse(nh.CHITIETNHAP.Rows[i]["MaNL"].ToString()), float.Parse(nh.CHITIETNHAP.Rows[i]["SoLuong"].ToString()), int.Parse(nh.CHITIETNHAP.Rows[i]["DonGia"].ToString()), int.Parse(nh.CHITIETNHAP.Rows[i]["ThanhTien"].ToString()), nh.MAKHO))
                    flag = false;
            }
            if (!flag)
            {
                nvDAO.XoaCTNhapHang(nh.MANHAPHANG);
            }
            return flag;
        }
    }
}
